package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.entity.UserPlanTimeEntity;
import com.ywjh.inter.service.UsersService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class BGluTime extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7721269940146238108L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");
		int r = 0;
		String e = "";
		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		JSONObject json = new JSONObject();
		switch (act) {
		case "set":
			int userId = data.getInt("user_id");
			String[] planId = data.getString("plan_array").split(", ");
			String[] start = data.getString("start_array").split(", ");
			String[] end = data.getString("end_array").split(", ");
			try {
				Timestamp addTime = new Timestamp(System.currentTimeMillis());
				List<UserPlanTimeEntity> list = new LinkedList<>();
				for (int i = 0; i < planId.length; i++) {
					UserPlanTimeEntity planTimeEntity = new UserPlanTimeEntity();
					planTimeEntity.setAdd_time(addTime);
					planTimeEntity.setEnd_hh(end[i]);
					planTimeEntity.setStart_hh(start[i]);
					planTimeEntity.setUser_id(userId);
					int plan_id = Integer.parseInt(planId[i]);
					planTimeEntity.setPlan_id(plan_id);
					list.add(planTimeEntity);
				}
				boolean b = UsersService.getInstance().setUserBGluTime(userId, list);
				if (b) {
					r = 1;
				} else {
					e = "服务器故障";
				}
			} catch (Exception e2) {
				e2.printStackTrace();
				e = "数据异常";
			}
			break;
		case "get":
			userId = data.getInt("user_id");
			List<UserPlanTimeEntity> list = UsersService.getInstance().getUserBGluTime(userId);
			if (list.isEmpty()) {
				// get default
				list = UsersService.getInstance().getUserBGluTime(-1);
			}
			JSONArray array = new JSONArray();
			for (UserPlanTimeEntity te : list) {
				JSONObject obj = new JSONObject();
				obj.put("id", te.getId());
				obj.put("user_id", te.getUser_id());
				obj.put("start_hh", te.getStart_hh());
				obj.put("end_hh", te.getEnd_hh());
				obj.put("plan_id", te.getPlan_id());
				obj.put("time", StrUtil.getYYYYMMDDHHMISS(te.getAdd_time()));
				array.add(obj);
			}

			r = 1;
			json.put("d", array);
			break;
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
